package com.zhhtech.zhh.ui.fragment

import android.os.Bundle
import android.view.View
import com.zhhtech.zhh.R
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.common.ABOUT
import com.zhhtech.zhh.common.AGREEMENT
import com.zhhtech.zhh.common.PRIVACY_POLICY
import com.zhhtech.zhh.common.UserCache
import com.zhhtech.zhh.databinding.FragmentMineBinding
import com.zhhtech.zhh.ui.activity.commom.AboutActivity
import com.zhhtech.zhh.ui.activity.commom.SettingActivity
import com.zhhtech.zhh.ui.activity.commom.openUrl
import com.zhhtech.zhh.ui.activity.main.CaseShareActivity
import com.zhhtech.zhh.ui.activity.main.CustomerServiceActivity
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseFragment
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.utils.GlideUtils

class MineFragment : BaseFragment<FragmentMineBinding>() {
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        bindViewClick(vb.rlKefu, vb.rlAnli, vb.rlCdbb, vb.ivSetting, vb.rlGuanyu, vb.rlYszc, vb.rlYhxy)
    }

    override fun onResume() {
        super.onResume()
        if (!isHidden) {
            changeUserData()
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            changeUserData()
        }
    }

    private fun changeUserData() {
        launchUI({
            val user = api.userInfo().toData() ?: return@launchUI
            UserCache.saveUserInfoBean(user)
            GlideUtils.intoCircle(requireContext(), R.mipmap.logo, vb.ivHead)
            if (user.phone.length == 11) {
                vb.tvPhoneNum.text = user.phone.substring(0, 3) + "****" + user.phone.substring(7, 11)
            } else {
                vb.tvPhoneNum.text = user.phone
            }
//            vb.tvInvitationCode.text = "邀请码：${user.inviteCode}"
//            vb.tvUserCount.text = user.inviteNum
//            vb.tvIncome.text = user.inviteIncome
        }, ApiObserver(complete = {
//            vb.refreshLayout.isRefreshing = false
        }))
    }

    override fun onClick(v: View?) {
        when (v) {
//            vb.tvInvitation, vb.tvInvitationCode -> UserCache.getUserInfoBean().inviteCode.clipboard()
//            vb.llIn -> startActivityExt<MyConsultationActivity>()
//            vb.llComplete -> startActivityExt<MyConsultationActivity> {
//                putExtra("index", 1)
//            }
//            vb.tvCountView -> startActivityExt<MyRecommendActivity>()
//            vb.tvWithdrawal -> startActivityExt<WithdrawActivity>()
            vb.rlKefu -> startActivityExt<CustomerServiceActivity>()
//            vb.rlYaoqing -> startActivityExt<InviteCodeActivity>()
            vb.rlAnli -> startActivityExt<CaseShareActivity>()
            vb.ivSetting -> startActivityExt<SettingActivity>()
            vb.rlYszc -> requireContext().openUrl(PRIVACY_POLICY)
            vb.rlYhxy -> requireContext().openUrl(AGREEMENT)
            vb.rlGuanyu -> context?.openUrl(ABOUT)
            vb.rlCdbb -> startActivityExt<AboutActivity>()
//            vb.rlShare -> startActivityExt<ShareActivity>()
        }
    }
}